<template>
  <div class="mask" v-if="showChart" @click="cartShowClick" />
  <div class="product" v-if="showChart">
    <div class="product__header">
      <div class="product__checkAll">
        <span class="product__checkAll__icon iconfont" @click="AllChange(id)">
          {{ cartEffect.chechAllChange ? "&#xe652;" : "&#xe619;" }}
        </span>
        全选
      </div>
      <div class="product__checkDeselectAll" @click="checkDeselectAllClick(id)">
        清空购物车
      </div>
    </div>
    <template v-for="item in cartEffect.getCartList" :key="item._id">
      <Commodity
        :show="true"
        :item="item"
        v-if="item.count > 0"
        :product__item__img__cart="true"
        :product__item__price__cart="true"
      >
        <template v-slot:chenckedSlot>
          <span
            class="product__item__checked iconfont"
            v-html="item.check ? '&#xe652;' : '&#xe619;'"
            @click="checkChangeClick(id, item._id)"
          />
        </template>
      </Commodity>
    </template>
  </div>
  <div class="cart">
    <div class="cart__icon">
      <img
        src="http://www.dell-lee.com/imgs/vue3/basket.png"
        alt=""
        class="cart__icon__img"
        @click="cartShowClick"
      />
      <span class="cart__icon__tag" v-if="cartEffect.total > 0">{{
        cartEffect.total
      }}</span>
    </div>
    <div class="cart__content" v-if="cartEffect.total > 0">
      总计：<span class="cart__content__price"
        >&yen;{{ cartEffect.price }}</span
      >
    </div>
    <div class="cart__content" v-else>购物车是空的</div>
    <div class="cart__btn" @click="closingClick">去结算</div>
  </div>
  <Toast v-if="show" :message="toastMessage" />
</template>

<script>
import { useRoute, useRouter } from "vue-router";
import { ref, watchEffect } from "vue";
import Commodity from "../../components/commodity/Commodity.vue";
import Toast from "../../components/toast/Toast.vue";
import { useToastEffect } from "../../components/toast/Toast.vue";
import { useCartEffect } from "../../components/cartCommon/commonCartEffect";

//购物车显示和隐藏
const useCartShowEffect = (cartEffect) => {
  const showChart = ref(false);

  const cartShowClick = () => {
    if (cartEffect.value.total > 0) showChart.value = !showChart.value;
  };
  watchEffect(() => {
    if (cartEffect.value.total <= 0) {
      showChart.value = false;
    }
  });
  return { showChart, cartShowClick };
};
const useRouterClose = (num, ToastShow) => {
  const router = useRouter();
  const route = useRoute();
  const id = route.params.id;
  const closingClick = () => {
    if (num.value.price <= 0) {
      ToastShow("购物车空空如也");
    } else {
      router.push(`/orderConfirmation/${id}`);
    }
  };
  return { closingClick };
};
export default {
  name: "shopCart",
  components: { Commodity, Toast },
  setup() {
    const {
      id,
      checkChangeClick,
      checkDeselectAllClick,
      cartEffect,
      AllChange,
    } = useCartEffect(); //
    const { showChart, cartShowClick } = useCartShowEffect(cartEffect);
    const { ToastShow, toastMessage, show } = useToastEffect();
    const { closingClick } = useRouterClose(cartEffect, ToastShow);
    return {
      id,
      checkChangeClick,
      checkDeselectAllClick,
      cartEffect,
      AllChange,
      cartShowClick,
      showChart,
      toastMessage,
      show,
      closingClick,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
@import "../../style/mixins.scss";
.mask {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
}
.cart {
  position: absolute;
  display: flex;
  left: 0;
  bottom: 0;
  height: 0.5rem;
  box-sizing: border-box;
  width: 100%;
  border-top: solid $content-bgColor 0.01rem;
  overflow-y: hidden;
  background-color: #fff;
  a {
    text-decoration: none;
  }
  &__icon {
    position: relative;
    display: flex;
    align-items: center;
    box-sizing: border-box;
    min-width: 0.84rem;
    padding-left: 0.24rem;
    &__img {
      width: 0.28rem;
      height: 0.26rem;
    }
    &__tag {
      position: absolute;
      left: 0.42rem;
      top: 0.04rem;
      display: inline-block;
      min-width: 0.2rem;
      padding: 0 0.02rem;
      height: 0.2rem;
      border-radius: 0.1rem;
      background-color: $color-e93b;
      line-height: 0.2rem;
      text-align: center;
      color: $color-fff;
      font-size: 0.12rem;
      transform: scale(0.5);
      transform-origin: left center;
    }
  }
  &__content {
    flex: 1;
    display: flex;
    align-items: center;
    font-size: 0.12rem;
    color: $content-fontcolor;
    &__price {
      font-size: 0.18rem;
      color: $color-e93b;
      line-height: 0.5rem;
    }
  }
  &__btn {
    width: 0.98rem;
    height: 0.5rem;
    background-color: #4fb0f9;
    color: $color-fff;
    line-height: 0.5rem;
    text-align: center;
  }
}
.product {
  position: absolute;
  bottom: 0.5rem;
  left: 0;
  box-sizing: border-box;
  width: 100%;
  overflow-y: scroll;
  background-color: #fff;
  &__header {
    display: flex;
    padding: 0 0.18rem;
    justify-content: space-between;
    align-items: center;
  }
  &__checkAll {
    font-size: 0.14rem;
    color: $content-fontcolor;
    height: 100%;
    line-height: 0.52rem;
    &__icon {
      font-size: 0.2rem;
      color: $color-0091ff;
      vertical-align: middle;
      margin-right: 0.08rem;
    }
  }
  &__checkDeselectAll {
    font-size: 0.14rem;
    color: $content-fontcolor;
  }
  &__header {
    height: 0.52rem;
    border-bottom: 0.01rem solid $content-bgColor;
  }
  &__item {
    border: none;
    padding: 0 0.18rem;
    &__img {
      width: 0.46rem;
      height: 0.46rem;
    }
    &__checked {
      display: flex;
      align-items: center;
      font-size: 0.2rem;
      color: $color-0091ff;
      margin-right: 0.19rem;
    }
  }
}
</style>